package com.atguigu.queue;

import org.jcp.xml.dsig.internal.SignerOutputStream;

import java.util.Scanner;

/**
 * @author durant
 * @create 2021-11-11 15:01
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue aq = new ArrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show)显示队列");
            System.out.println("a(add)显示队列");
            System.out.println("y(isEmpty)显示队列");
            System.out.println("f(isFull)显示队列");
            System.out.println("e(exit)显示队列");
            System.out.println("g(get)出队列");
            System.out.println("h(head)出队列");
            System.out.println("****************");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    aq.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数：");
                    int value =scanner.nextInt();
                    aq.addQueue(value);

                    break;
                case 'y':
                    System.out.println(aq.isEmpty());

                    break;
                case 'f':
                    System.out.println("队列是否为空："+aq.isFull());
                    break;

                case 'g':
                    try {
                        System.out.println("取出的数据是："+aq.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    System.out.println(aq.headQueue());
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    System.out.println("程序退出了");
                    break;
                default:
                    break;

            }


        }


    }
}

//使用数组模拟队列-编写一个ArrayQueue类
class ArrayQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;//该数组用于存放数据
    //创建队列的构造器

    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;//指向队列头部，分析出front是指向队列的前一个位置
        rear = -1;//指向队列尾部
    }

    //判断队列是否满
    public boolean isFull() {

        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty() {

        return rear == -1;
    }

    //添加数据到队列
    public void addQueue(int n) {
        //判断队列是否满
        if (isFull()) {
            System.out.println("队列满了");
        }
        rear++;
        arr[rear] = n;
    }

    //出队列，从前（front）出数据
    public int getQueue() {
        //判断队列是否空
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
        front++;
        return arr[front];
    }

    //显示队列所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，无法遍历");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print("\t" + arr[i]);
        }
        System.out.println();
    }

    //显示队列的头数据，不是取数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，没有头数据");
        }
        return arr[front + 1];
    }
}
